var sounddb = require('../../db/sound.db')
var orderdb = require('../../db/order.db')
var logger = require('../../logger')
var config = require('../../config')
var emit = require('../../emit')
var async = require('async');

//获取购物车数据列表
function onsocket(socket,data){
    async.waterfall([
        function (callback){
            orderdb.getallcarts(socket.handshake.cookies.authorized, (err,response) =>{
                callback(err,response);
            });
        },
        function (response, callback) {
            var results = new Array();
            async.forEach(response, (r, cb) => {
                sounddb.getfileinfo(r, function(err, info){
                    if(info != null){
                        results.push(info);
                    }
                    cb(err);
                });
            }, (err) => {
                if (err) {
                    callback(err);
                }
                else {
                    callback(null, results);
                }
            })
        }
    ],(err,results) =>{
        if (err) {
            logger.log(err);
        } else {
            logger.log(results);
            var data = { results: results };
            if (config.debug) {
                logger.log(data);
            } else {
                emit.send(socket,'getcartlist', data);
            }
        }
    });
}

module.exports = {
    onsocket
}